package com.cheneyjoo.dao;

import com.cheneyjoo.model.User;
import com.cheneyjoo.util.Repository;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class UserDao {
    private static List<User> userList = new ArrayList<>();

    /**
     * 初始化数据信息
     *
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public UserDao() throws IOException, ClassNotFoundException {
        File repositoryFile = Repository.getRepositoryFile("Exam.User.txt");
        if (userList.size() == 0 && repositoryFile.length() != 0) {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(repositoryFile));
            userList = (List<User>) ois.readObject();
            ois.close();
        }
    }

    /**
     * 获取用户
     *
     * @param account
     * @return
     */
    public User getUser(String account) {
        Optional<User> user = userList.stream().filter(s -> account.equals(s.getAccount())).findFirst();
        return user.orElse(null);
    }

    /**
     * 通过查询条件获取用户信息
     *
     * @param account
     * @return
     */
    public List<User> getUsersByAccount(String account) {
        List<User> users = userList.stream().filter(s -> s.getAccount().contains(account) || s.getName().contains(account)).collect(Collectors.toList());
        return users;
    }

    /**
     * 获取用户
     *
     * @param id
     * @return
     */
    public User getUser(int id) {
        Optional<User> user = userList.stream().filter(s -> id == s.getId()).findFirst();
        return user.orElse(null);
    }

    /**
     * 获取全部用户信息
     *
     * @return
     */
    public List<User> getUserList() {
        return userList;
    }

    /**
     * 添加用户
     *
     * @param user
     * @return
     */
    public boolean addUser(User user) {
        Optional<User> userOption = userList.stream().filter(s -> user.getAccount().equals(s.getAccount())).findFirst();
        if (userOption.isPresent()) return false;
        Optional<Integer> max = userList.stream().map(s -> s.getId()).max((o1, o2) -> o1.compareTo(o2));
        user.setId(max.orElse(0) + 1);
        user.setType(2);
        userList.add(user);
        FlushData();
        return true;
    }

    /**
     * 修改用户信息
     *
     * @param user
     * @return
     */
    public boolean updateUser(User user) {
        Optional<User> userOption = userList.stream().filter(s -> user.getId() == s.getId()).findFirst();
        Optional<User> checkAccountExists = userList.stream().filter(s -> user.getId() != s.getId() && user.getAccount().equals(s.getAccount())).findFirst();
        if (checkAccountExists.isPresent()) {
            return false;
        }
        userOption.ifPresent(s -> {
            s.setName(user.getName());
            s.setAccount(user.getAccount());
            s.setPassword(user.getPassword());
        });
        FlushData();
        return userOption.isPresent();
    }

    /**
     * 删除用户
     *
     * @param id
     * @return
     */
    public boolean deleteUser(int id) {
        boolean remove = userList.remove(userList.stream().filter(s -> id == s.getId()).findFirst().orElse(null));
        if (remove) {
            FlushData();
        }
        return remove;
    }

    /**
     * 修改密码
     * @param id
     * @param oldPwd
     * @param newPwd
     * @return
     */
    public boolean updatePwd(int id, String oldPwd, String newPwd) {
        Optional<User> user = userList.stream().filter(s -> s.getId() == id).findFirst();
        if(user.isPresent()){
            User getUser=user.get();
            if(getUser.getPassword().equals(oldPwd)){
                getUser.setPassword(newPwd);
                FlushData();
                return true;
            }else {
                return false;
            }
        }else {
            return false;
        }
    }

    private void FlushData() {
        try {
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(Repository.getRepositoryFile("Exam.User.txt")));
            oos.writeObject(userList);
            oos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
